/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mrp.persistencia.controller;

import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.transaction.UserTransaction;
import mrp.persistencia.controller.exceptions.NonexistentEntityException;
import mrp.persistencia.controller.exceptions.PreexistingEntityException;
import mrp.persistencia.controller.exceptions.RollbackFailureException;
import mrp.persistencia.entity.Talla;
import mrp.persistencia.entity.Detallepedido;
import mrp.persistencia.entity.Detalletalla;

/**
 *
 * @author root
 */
public class DetalletallaJpaController implements Serializable {

    public DetalletallaJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Detalletalla detalletalla) throws PreexistingEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Talla tallaFk = detalletalla.getTallaFk();
            if (tallaFk != null) {
                tallaFk = em.getReference(tallaFk.getClass(), tallaFk.getCodtalla());
                detalletalla.setTallaFk(tallaFk);
            }
            Detallepedido detallepedidoFk = detalletalla.getDetallepedidoFk();
            if (detallepedidoFk != null) {
                detallepedidoFk = em.getReference(detallepedidoFk.getClass(), detallepedidoFk.getCoddetalle());
                detalletalla.setDetallepedidoFk(detallepedidoFk);
            }
            em.persist(detalletalla);
            if (tallaFk != null) {
                tallaFk.getDetalletallaList().add(detalletalla);
                tallaFk = em.merge(tallaFk);
            }
            if (detallepedidoFk != null) {
                detallepedidoFk.getDetalletallaList().add(detalletalla);
                detallepedidoFk = em.merge(detallepedidoFk);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            if (findDetalletalla(detalletalla.getCoddetalletalla()) != null) {
                throw new PreexistingEntityException("Detalletalla " + detalletalla + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Detalletalla detalletalla) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Detalletalla persistentDetalletalla = em.find(Detalletalla.class, detalletalla.getCoddetalletalla());
            Talla tallaFkOld = persistentDetalletalla.getTallaFk();
            Talla tallaFkNew = detalletalla.getTallaFk();
            Detallepedido detallepedidoFkOld = persistentDetalletalla.getDetallepedidoFk();
            Detallepedido detallepedidoFkNew = detalletalla.getDetallepedidoFk();
            if (tallaFkNew != null) {
                tallaFkNew = em.getReference(tallaFkNew.getClass(), tallaFkNew.getCodtalla());
                detalletalla.setTallaFk(tallaFkNew);
            }
            if (detallepedidoFkNew != null) {
                detallepedidoFkNew = em.getReference(detallepedidoFkNew.getClass(), detallepedidoFkNew.getCoddetalle());
                detalletalla.setDetallepedidoFk(detallepedidoFkNew);
            }
            detalletalla = em.merge(detalletalla);
            if (tallaFkOld != null && !tallaFkOld.equals(tallaFkNew)) {
                tallaFkOld.getDetalletallaList().remove(detalletalla);
                tallaFkOld = em.merge(tallaFkOld);
            }
            if (tallaFkNew != null && !tallaFkNew.equals(tallaFkOld)) {
                tallaFkNew.getDetalletallaList().add(detalletalla);
                tallaFkNew = em.merge(tallaFkNew);
            }
            if (detallepedidoFkOld != null && !detallepedidoFkOld.equals(detallepedidoFkNew)) {
                detallepedidoFkOld.getDetalletallaList().remove(detalletalla);
                detallepedidoFkOld = em.merge(detallepedidoFkOld);
            }
            if (detallepedidoFkNew != null && !detallepedidoFkNew.equals(detallepedidoFkOld)) {
                detallepedidoFkNew.getDetalletallaList().add(detalletalla);
                detallepedidoFkNew = em.merge(detallepedidoFkNew);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                String id = detalletalla.getCoddetalletalla();
                if (findDetalletalla(id) == null) {
                    throw new NonexistentEntityException("The detalletalla with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(String id) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Detalletalla detalletalla;
            try {
                detalletalla = em.getReference(Detalletalla.class, id);
                detalletalla.getCoddetalletalla();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The detalletalla with id " + id + " no longer exists.", enfe);
            }
            Talla tallaFk = detalletalla.getTallaFk();
            if (tallaFk != null) {
                tallaFk.getDetalletallaList().remove(detalletalla);
                tallaFk = em.merge(tallaFk);
            }
            Detallepedido detallepedidoFk = detalletalla.getDetallepedidoFk();
            if (detallepedidoFk != null) {
                detallepedidoFk.getDetalletallaList().remove(detalletalla);
                detallepedidoFk = em.merge(detallepedidoFk);
            }
            em.remove(detalletalla);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Detalletalla> findDetalletallaEntities() {
        return findDetalletallaEntities(true, -1, -1);
    }

    public List<Detalletalla> findDetalletallaEntities(int maxResults, int firstResult) {
        return findDetalletallaEntities(false, maxResults, firstResult);
    }

    private List<Detalletalla> findDetalletallaEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Detalletalla.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Detalletalla findDetalletalla(String id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Detalletalla.class, id);
        } finally {
            em.close();
        }
    }

    public int getDetalletallaCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Detalletalla> rt = cq.from(Detalletalla.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
